<?php
if(checarUsuario(0)){
	?>
<script
	type="text/javascript" src="../js/usuario.js"></script>
<div class="row">
	<div class="col-lg-12">
		<h3 class="page-header">
			<b>Usuários</b>
		</h3>
	</div>
</div>
<div class="row">
	<div class="col-lg-12">
		<form action="index.php?pag=usuario/consulta" method="POST">
			<div class="panel panel-primary">
				<div class="panel-heading">Busca</div>
				<div class="panel-body">
					<div class="row">
						<div class="col-lg-3">
							<select class="form-control" name="tdados" id="tdados">
							<option value="1">Login</option>
							<option value="2">Nome</option>
							</select>
						</div>
						<div class="col-lg-6">
							<input class="form-control"	name="dados" id="dados" />
						</div>
						<div class="col-lg-3">
							<button class="btn btn-primary btn-sm" type="submit">
								<span class="glyphicon glyphicon-search"></span> Buscar
							</button>
						</div>
					</div>
				</div>
			</div>
		</form>
	</div>
</div>
<div class="row">
	<div class="col-lg-12">
		<table class="table">
			<thead>
				<tr>
					<th>Login</th>
					<th>Nome</th>
					<th>Perfil</th>
					<th>Ação</th>
				</tr>
			</thead>
			<tbody>
			<?php
			$where = '';
			if(isset($_POST['dados'])){
				$dados = $_POST['dados'];
				if($_POST['tdados'] == 1){
					$where .= "where login like '%$dados%'";
				}else{
					$where .= "where nome like '%$dados%'";
				}
			}
			
			$query = "select * from usuario $where order by nome";
			$sql_usuario = $conn->query($query);
			while ($user = $sql_usuario->fetch(PDO::FETCH_OBJ)){
				?>
				<tr>
					<td><?php echo $user->login; ?></td>
					<td><?php echo $user->nome; ?></td>
					<td><?php 
					if($user->papel == 0){
						echo 'Administrador';
					}else if($user->papel == 1){
						echo 'Editor';
					}else{
						echo 'Leitor';
					} ?>
					</td>
					<td><?php echo $user->situacao == 'P' ? '<button codigo="'.$user->login.'" class="btn btn-success btn-xs" title="Ativar" onclick="mudarEstado(this,\'A\')"><span class="glyphicon glyphicon-ok-sign"></span></button>' : '<button codigo="'.$user->login.'" class="btn btn-danger btn-xs" title="Desativar" onclick="mudarEstado(this,\'D\')"><span class="glyphicon glyphicon-remove-sign"></span></button>'; ?>
						<a class="btn btn-default btn-xs" title="Editar"
						href="index.php?pag=usuario/altera&cod=<?php echo base64_encode($user->login)?>"><span
							class="glyphicon glyphicon-pencil"></span> </a> <a
						class="btn btn-danger btn-xs" title="Excluir" href="model/usuario_remove.php?codcry=<?php echo base64_encode($user->login)?>""><span
							class="glyphicon glyphicon-remove"></span> </a>
					</td>
				</tr>
				<?php } ?>
			</tbody>
		</table>
	</div>
</div>
				<?php }else{
					$_SESSION['msg_erro'] = "Usuário não autorizado, contate o administrador.";
					echo '<script>window.location="index.php"</script>';
				}?>